package cn.eeepay.framework.service.impl;

import cn.eeepay.framework.dao.MerTerChangeBindDao;
import cn.eeepay.framework.dao.SysDictDao;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.MerTerChangeBind;
import cn.eeepay.framework.model.Result;
import cn.eeepay.framework.model.SysDict;
import cn.eeepay.framework.model.TerminalInfo;
import cn.eeepay.framework.service.MerTerChangeBindService;
import cn.eeepay.framework.util.ClientInterface;
import cn.eeepay.framework.util.Constants;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.internal.org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("merTerChangeBindService")
@Transactional
public class MerTerChangeBindServiceImpl implements MerTerChangeBindService {

    @Resource
    private MerTerChangeBindDao merTerChangeBindDao;

    @Resource
    private SysDictDao sysDictDao;

    @Override
    public List<MerTerChangeBind> selectMerTerChangeBindPage(Page<MerTerChangeBind> page, MerTerChangeBind baseInfo) {
        return merTerChangeBindDao.selectMerTerChangeBindPage(page, baseInfo);
    }

    @Override
    public MerTerChangeBind selectMerTerChangeBindById(String id) {
        return merTerChangeBindDao.selectMerTerChangeBindById(id);
    }

    @Override
    public int updateChangeStatus(MerTerChangeBind merTerChangeBind) {
        return merTerChangeBindDao.updateChangeStatus(merTerChangeBind);
    }

    @Override
    public TerminalInfo selectBySnAndMerNo(String sn, String merchantNo) {
        return merTerChangeBindDao.selectBySnAndMerNo(sn,merchantNo);
    }

    @Override
    public Result toBindingTerminal(MerTerChangeBind merTerChangeBind) {
        Result result = new Result();
        String coreUrl = "";
        SysDict coreUrlDict = sysDictDao.getByKey("CORE_URL");

        if(coreUrlDict != null){
            coreUrl = coreUrlDict.getSysValue() + Constants.BIND_OR_UNBIND;
        }
        //调用core接口进行绑定
        String returnStr = ClientInterface.toUpperBindTerminal(coreUrl, merTerChangeBind.getOldSn(), merTerChangeBind.getMerchantNo(), merTerChangeBind.getMbpid(), "0");
        if (StringUtils.isNotBlank(returnStr)) {
            JSONObject json = JSONObject.parseObject(returnStr);
            JSONObject headJson = JSONObject.parseObject(json.getString("header"));
            if (headJson.getBoolean("succeed")) {
                //回绑成功
                merTerChangeBind.setChangeStatus("4");
                merTerChangeBind.setRebindRemark(headJson.getString("errMsg"));
                updateChangeStatus(merTerChangeBind);

                result.setStatus(true);
                result.setMsg(headJson.getString("errMsg"));

            }else{
                //回绑失败
                merTerChangeBind.setRebindRemark(headJson.getString("errMsg"));
                merTerChangeBindDao.updateRebindRemark(merTerChangeBind);
                result.setMsg(headJson.getString("errMsg"));
            }
        } else {
            result.setMsg("调用接口失败");
        }

        return result;
    }
}
